#!/usr/bin/env python
# -*- coding: utf-8 -*-
import wx
import itertools
from assis_cloud import *
from assis_const import *

class CloudSettings(wx.Frame):

	def __init__(self, config, google, *args, **kwds):

		kwds["style"]			  = wx.DEFAULT_FRAME_STYLE | wx.TAB_TRAVERSAL | wx.WANTS_CHARS
		wx.Frame.__init__(self, *args, **kwds)
		self.statusbar			  = self.CreateStatusBar(1, 0)
		self.static_line_5		  = wx.StaticLine(self, -1)
		self.static_line_3		  = wx.StaticLine(self, -1, style=wx.LI_VERTICAL)
		self.label_credential	  = wx.StaticText(self, -1, "Google credential")
		self.label_name			  = wx.StaticText(self, -1, "Name")
		self.label_pass 		  = wx.StaticText(self, -1, "Password")
		self.checkbox_credential  = wx.CheckBox(self, -1, "")
		self.textctrl_name 		  = wx.TextCtrl(self, -1, config.g_name)
		self.textctrl_pass 		  = wx.TextCtrl(self, -1, config.g_pass, style = wx.TE_PASSWORD)
		self.button_connect 	  = wx.Button(self, -1, "Connect")
		self.static_line_4		  = wx.StaticLine(self, -1, style=wx.LI_VERTICAL)
		self.static_line_1		  = wx.StaticLine(self, -1)
		self.static_line_2		  = wx.StaticLine(self, -1)
		self.static_line_7 		  = wx.StaticLine(self, -1, style=wx.LI_VERTICAL)
		self.label_spreadsheets   = wx.StaticText(self, -1, "Spreadsheets")
		self.choice				  = wx.Choice(self, -1, choices=[config.g_spreadsheet])
		self.checkbox_spreadsheet = wx.CheckBox(self, -1, "")
		self.button_open		  = wx.Button(self, -1, "Open")
		self.static_line_8		  = wx.StaticLine(self, -1, style=wx.LI_VERTICAL)
		self.static_line_6		  = wx.StaticLine(self, -1)
		self.config				  = config
		self.google				  = google
		self.controls			  = itertools.cycle([self.textctrl_name, self.textctrl_pass, \
													 self.checkbox_credential, self.button_connect, self.choice, \
													 self.checkbox_spreadsheet, self.button_open])
		self.button_connect.Bind(wx.EVT_BUTTON, self.connect)
		self.button_open.Bind(wx.EVT_BUTTON, self.open)
		self.checkbox_credential.Bind(wx.EVT_CHECKBOX, self.toggle_sp_check)
		self.Bind(wx.EVT_NAVIGATION_KEY, self.traverse)
		
		self.__set_properties()
		self.__do_layout()
		
	def traverse(self, event):
		
		control = next(self.controls)
		control.SetFocus()
		
	def connect(self, event):
		
		name		= self.textctrl_name.GetValue()
		passwd		= self.textctrl_pass.GetValue()

		if len(name) == 0 or len(passwd) < MIN_PASSWD_LENGTH:
			self.popup_warning("Please enter a valid name and password !")
			return False
		
		self.google.connect(name, passwd)
		self.populate_choice()	
	
	def open(self, event):

		name		= self.textctrl_name.GetValue()
		passwd		= self.textctrl_pass.GetValue()
		spreadsheet = self.choice.GetStringSelection()
		if len(spreadsheet) < 1:
			self.popup_warning("Please select a spreadsheet !")
			return False
			
		self.google.set_spreadsheet(spreadsheet, self.choice.GetSelection())
		self.config.save_in_obj(name, passwd, spreadsheet)
		if self.isacc_default():
			if not self.issp_default():
				spreadsheet = None
			self.config.save_settings(name, passwd, spreadsheet)
			
		self.Close()
		return True
		
	def populate_choice(self):
		
		self.choice.Clear()
		for item in self.google.get_spreadsheets():
			if (len(item) > 0):
				self.choice.Append(item)
		return True
 
	def toggle_sp_check(self, event):
		
		if self.checkbox_credential.IsChecked():
			self.checkbox_spreadsheet.Enable()
		else:
			self.checkbox_spreadsheet.SetValue(False)
			self.checkbox_spreadsheet.Disable()
 
	def isacc_default(self):
		
		return self.checkbox_credential.IsChecked()
 
	def issp_default(self):
		
		return self.checkbox_spreadsheet.IsChecked()
 
	def popup_warning(self, message, caption = 'Warning!'):
		dlg = wx.MessageDialog(self, message, caption, wx.OK | wx.ICON_WARNING)
		dlg.ShowModal()
		dlg.Destroy()
		
	def __set_properties(self):
		
		self.SetTitle("Cloud Settings")
		self.SetSize((450, 350))
		self.statusbar.SetStatusWidths([-1])
		
		statusbar_fields = ["frame_2_statusbar"]
		for i in range(len(statusbar_fields)):
			self.statusbar.SetStatusText(statusbar_fields[i], i)
		self.static_line_5.SetMinSize((430, 2))
		self.label_credential.SetMinSize((170, 25))
		self.label_credential.SetFont(wx.Font(11, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
		self.label_name.SetMinSize((150, 25))
		self.label_pass.SetMinSize((150, 22))
		self.checkbox_credential.SetToolTipString("Make default")
		self.textctrl_name.SetMinSize((150, 25))
		self.textctrl_pass.SetMinSize((150, 25))
		self.button_connect.SetMinSize((70, 25))
		self.static_line_1.SetMinSize((430, 2))
		self.static_line_2.SetMinSize((430, 2))
		self.label_spreadsheets.SetMinSize((150, 25))
		self.label_spreadsheets.SetFont(wx.Font(11, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
		self.choice.SetMinSize((170, 21))
		self.checkbox_spreadsheet.SetToolTipString("Make default")
		self.button_open.SetMinSize((70, 25))
		self.static_line_6.SetMinSize((430, 2))

	def __do_layout(self):

		self.textctrl_pass.MoveAfterInTabOrder(self.textctrl_name)
		self.checkbox_spreadsheet.Disable()
		self.textctrl_name.SetFocus()
		sizer_1 = wx.BoxSizer(wx.VERTICAL)
		sizer_7 = wx.BoxSizer(wx.HORIZONTAL)
		sizer_12 = wx.BoxSizer(wx.VERTICAL)
		sizer_13 = wx.BoxSizer(wx.HORIZONTAL)
		sizer_2 = wx.BoxSizer(wx.HORIZONTAL)
		sizer_3 = wx.BoxSizer(wx.VERTICAL)
		sizer_4 = wx.BoxSizer(wx.HORIZONTAL)
		sizer_11 = wx.BoxSizer(wx.HORIZONTAL)
		sizer_5 = wx.BoxSizer(wx.HORIZONTAL)
		sizer_6 = wx.BoxSizer(wx.HORIZONTAL)
		sizer_1.Add(self.static_line_5, 0, wx.TOP|wx.ALIGN_CENTER_HORIZONTAL, 10)
		sizer_2.Add(self.static_line_3, 0, wx.LEFT|wx.EXPAND, 10)
		sizer_6.Add(self.label_credential, 0, wx.LEFT|wx.TOP, 10)
		sizer_3.Add(sizer_6, 1, wx.EXPAND, 0)
		sizer_5.Add(self.label_name, 0, wx.LEFT|wx.ALIGN_CENTER_VERTICAL, 10)
		sizer_5.Add(self.label_pass, 0, wx.LEFT|wx.ALIGN_CENTER_VERTICAL, 5)
		sizer_5.Add(self.checkbox_credential, 0, wx.LEFT|wx.ALIGN_CENTER_VERTICAL, 5)
		sizer_3.Add(sizer_5, 1, wx.EXPAND, 0)
		sizer_4.Add(self.textctrl_name, 0, wx.LEFT|wx.BOTTOM, 10)
		sizer_4.Add(self.textctrl_pass, 0, wx.LEFT|wx.BOTTOM, 5)
		sizer_11.Add(self.button_connect, 0, wx.LEFT|wx.BOTTOM, 5)
		sizer_4.Add(sizer_11, 1, wx.EXPAND, 0)
		sizer_3.Add(sizer_4, 1, wx.EXPAND, 0)
		sizer_2.Add(sizer_3, 1, wx.EXPAND, 0)
		sizer_2.Add(self.static_line_4, 0, wx.RIGHT|wx.EXPAND, 10)
		sizer_1.Add(sizer_2, 1, wx.EXPAND, 0)
		sizer_1.Add(self.static_line_1, 0, wx.BOTTOM|wx.ALIGN_CENTER_HORIZONTAL, 10)
		sizer_1.Add(self.static_line_2, 0, wx.TOP|wx.ALIGN_CENTER_HORIZONTAL, 10)
		sizer_7.Add(self.static_line_7, 0, wx.LEFT|wx.EXPAND, 10)
		sizer_13.Add(self.label_spreadsheets, 0, wx.LEFT|wx.TOP, 10)
		sizer_13.Add(self.choice, 0, wx.TOP, 10)
		sizer_13.Add(self.checkbox_spreadsheet, 0, wx.LEFT|wx.TOP, 10)
		sizer_12.Add(sizer_13, 1, wx.EXPAND, 0)
		sizer_12.Add(self.button_open, 0, wx.BOTTOM|wx.ALIGN_CENTER_HORIZONTAL, 40)
		sizer_7.Add(sizer_12, 1, wx.EXPAND, 0)
		sizer_7.Add(self.static_line_8, 0, wx.RIGHT|wx.EXPAND, 10)
		sizer_1.Add(sizer_7, 1, wx.EXPAND, 0)
		sizer_1.Add(self.static_line_6, 0, wx.BOTTOM|wx.ALIGN_CENTER_HORIZONTAL, 10)
		self.SetSizer(sizer_1)
		self.Layout()
		self.Centre()
